<?php

class CategoryController extends  CommonController{
	
	private $_model;
	
	public function __init(){
		
		$this->_model=K('category');
	}
	
	public function index(){
		
		$data=$this->_model->get_all_data();
//		p($data);
		$this->assign('data',$data);
		
		
		$this->display();
	}
	
	public function add(){
		
		if(IS_POST){
			if(!$this->_model->add_data()) $this->error($this->_model->error);
			$this->success('添加成功');
		}
		
		$this->display();
	}
	
	
	public function del(){
		
		if(!$this->_model->delete_data(array('cid'=>Q('get.cid')))) $this->error($this->_model->error);
			$this->success('删除成功');
	}
	
	
	public function add_son(){
		
		if(IS_POST){
					
			if(!$this->_model->add_son_data()) $this->error($this->_model->error);
			
			$this->success('添加成功');
		}
		
		$data=K('Type')->get_all_data();
		
		$this->assign('data',$data);
		
		$this->display();
	}
	
	public function edit(){
		
		$where=array('cid'=>Q('get.cid'));
		if(IS_POST){
			
			if(!$this->_model->edit_data($where)) $this->error($this->_model->error);
			
			$this->success('修改成功');
		}
		
		$type=K('Type')->get_all_data();
		$date=$this->_model->get_one_data($where);
		$tid=$this->_model->get_tid($where);
		
		$this->assign('date',$date);
		$this->assign('type',$type);
		$this->assign('tid',$tid);
		$this->display();
	}
	
	public function ajax_text(){
		
		echo Q('post.cid');
	}
	
	
	public function get_all_data($where=null){
		
		return $this->where($where)->all();
		
	}
	
	
	
}












